package com.cooqe.snmp.snmpfinder;

import com.alibaba.fastjson.JSONObject;
import com.cooqe.basefinder.AbstractBaseFinder;
import com.cooqe.entity.ItMetric;
import com.cooqe.snmp.model.SnmpParameter;
import com.cooqe.snmp.utils.CommonUtil;
import com.cooqe.snmp.utils.SnmpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/14-2023
 * @Description: snmp 进行采集
 */
@Component(value = "snmpFinderJob")
@Slf4j
public class SnmpFinderJob extends AbstractBaseFinder {

    @Autowired
    private SnmpGetHandle snmpGetHandle;

    @Override
    //@XxlJob("snmpFinderJob")
    public void schedulingExecute(String params) {
        super.schedulingExecute(params);
    }

    @Override
    public void beforeCollection(JSONObject jsonObject) {
        log.info("beforeCollection 方法执行......");
        //if (this.threadLocal.get()==null) {return;}
        //模版参数
        String ip = jsonObject.getString("ip");
        Integer port = jsonObject.getInteger("port");
        Integer version = jsonObject.getInteger("version");
        String community = jsonObject.getString("community");
        SnmpParameter snmpParameter = new SnmpParameter();
        snmpParameter.setIp(ip);
        snmpParameter.setPort(port);
        snmpParameter.setVersion(version);
        snmpParameter.setCommunity(community);
        Target target = SnmpUtil.createTarget(snmpParameter, SnmpUtil.createSnmp());

        //封装 threadLocal
        Map<String, Object> map = new HashMap<>();
        map.put("snmpParameter", snmpParameter);
        map.put("target", target);
        threadLocal.set(map);
    }

    @Override
    public void doCollection(List<ItMetric> metricList) {
        //参数
        Map<String, Object> paramMap = threadLocal.get();
        SnmpParameter snmpParameter = (SnmpParameter) paramMap.get("snmpParameter");
        Target target = (Target) paramMap.get("target");
        Snmp snmp = (Snmp) paramMap.get("target");

        //依次采集每一个指标
        for (ItMetric metric : metricList) {
            String collectParams = metric.getCollectParams();
            if (StringUtils.isNotBlank(collectParams)) {
                JSONObject collectParamsObj = JSONObject.parseObject(collectParams);
                String oids = collectParamsObj.getString("oids");
                String method = collectParamsObj.getString("method");
                try {
                    switch (CommonUtil.getSnmpMethodCode(method)) {
                        case PDU.GET:
                            snmpGetHandle.snmpGet(oids, snmpParameter, target,snmp);
                            break;
                        case PDU.GETNEXT:
                            log.info("snmp getNext ...");
                            break;
                        case PDU.GETBULK:
                            log.info("snmp getBulk ...");
                            break;
                        // ......
                        default:
                            log.info("default ...");
                    }
                }catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        }
    }
}